package cn.IT.service.impl;

import java.io.Serializable;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.functors.ForClosure;
import org.apache.jasper.tagplugins.jstl.core.If;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.IT.dao.ILikeDAO;
import cn.IT.dao.IReplyDAO;
import cn.IT.dao.ITopicDAO;
import cn.IT.dao.IUserDAO;
import cn.IT.domain.Like;
import cn.IT.domain.Reply;
import cn.IT.domain.Topic;
import cn.IT.domain.Type;
import cn.IT.domain.User;
import cn.IT.service.IReplyService;
import cn.IT.service.ITopicService;
import cn.IT.service.IUserService;
import net.sf.json.JSONObject;

@Service(value = "replyService")
@SuppressWarnings("all")
@Transactional
public class ReplyService implements IReplyService {

	@Resource
	private IReplyDAO replyDao;
	@Resource
	private IUserDAO userDao;
	@Resource
	private ITopicDAO topicDao;
	@Resource
	private ILikeDAO likeDao;

	@Override
	public List<Reply> findByTopicId(Serializable topicId) {
		List<Reply> reply = replyDao.findByTopicId(topicId);
		if(reply != null){
			Collections.reverse(reply);
		}
		return reply;
		
	}
	
	//更新评论的点赞数量及用户列表
	@Override
	public Reply updateReplyLike(Serializable replyId,Serializable userId){
		Reply reply = replyDao.get(Reply.class, replyId);
		User user = userDao.get(User.class, userId);
		if (reply!=null){
			reply.setLikeCount(reply.getLikeCount()+1);
			replyDao.update(reply);
			Like like = new Like();
			like.setReply(reply);
			like.setUser(user);
			likeDao.save(like);
		}
		return reply;
	}
	
	@Override
	public Serializable addReply(Reply reply){
		
		//更新用户的评论数量
		User user = userDao.get(User.class, reply.getUser().getUserId());
		user.setReplyCount(user.getReplyCount()+1);
		userDao.update(user);
		
		//更新帖子的评论数量
		Topic topic = topicDao.get(Topic.class, reply.getTopic().getTopicId());
		topic.setReplyCount(topic.getReplyCount()+1);
		topicDao.update(topic);
		
		reply.setLikeCount(0);
		reply.setCreatedOn(new Date());
		reply.setIsReaded(0);
		
		return replyDao.save(reply);
	}
	
	@Override
	public List<Reply> findAllReplyByUser(User user) {
		List<Reply> reply = replyDao.findByUser(user);
		if(reply != null){
			Collections.reverse(reply);
		}
		return reply;
		
	}
	
	@Override
	public List<Reply> findNewsByTopicUser(User user) {
		List<Reply> reply = replyDao.findNewsByTopicUser(user);
		if(reply != null){
			Collections.reverse(reply);
		}
		return reply;
		
	}
	
	@Override
	public String updateNewsByTopicUser(User user) {
		List<Reply> reply = replyDao.findNewsByTopicUser(user);
		Reply updateReply;
		try {
			if(reply != null){
			   for(int i=0;i<reply.size();i++){
				   updateReply = reply.get(i);
				   if (updateReply.getIsReaded() == 0){
					   updateReply.setIsReaded(1);
					   replyDao.update(updateReply);
				   }
			   }
			}
			return "success";
		} catch (Exception e) {
			return "error";
		}	
	}

	@Override
	public String delReply(Reply reply,User user) {
		User updateuser;
		try {
			
			
			//更新帖子的评论数量
			Reply reply2 = replyDao.get(Reply.class, reply.getReplyId());
			Topic topic = reply2.getTopic();
			if(topic != null){
	            topic.setReplyCount(topic.getReplyCount()-1);
	            topicDao.update(topic);
			}
            //更新用户的评论数量
			updateuser = userDao.get(User.class, user.getUserId());
			if (updateuser != null){
				updateuser.setReplyCount(updateuser.getReplyCount()-1);
				userDao.update(updateuser);
			}
			
			replyDao.delete(Reply.class,reply.getReplyId());
			return "success";
			
		} catch (Exception e) {
			System.out.println(e);
			return "error";
		}
	}
	
	
	

}
